package dao;

import java.util.Date;
import java.util.List;

import javax.ejb.CreateException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import entity.Employee;
import entity.PaymentReport;
import entity.SalesCard;

@Stateless
public class PaymentReportDao {

	@PersistenceContext 
	EntityManager manager;
	
	
	public void createPaymentReport(PaymentReport report){
		manager.persist(report);
	}
	public List<PaymentReport> EmployeePaymentReports(Employee employee){
		String spql = "Select p from PaymentReport p where p.employee='" + employee.getId() + "'";
		TypedQuery<PaymentReport> query = manager.createQuery(spql,PaymentReport.class);
		List<PaymentReport> listReports = query.getResultList();
		return listReports;
		
	}
	public int deletePaymentReport(PaymentReport report){
		try {
			PaymentReport deletedReport=manager.merge(report);
			manager.remove(deletedReport);	
		}
		catch (Exception e){
			return -1;
		}
		return 0;
	}
	public Date lastPayDay(Employee employee){
		Date date=null;
		String spql = "Select d from PaymentReport d where d.employee='" + employee.getId() + "'  order by d.PaymentDate desc";
		TypedQuery<PaymentReport> query = manager.createQuery(spql,PaymentReport.class);
		List<PaymentReport> listReports = query.getResultList();
		if(listReports.size()!=0){
			date=listReports.get(0).getPaymentDate();
		}
		return date;
	}
}
